package com.vietnam.power.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vietnam.power.dao.ApplicationDao;
import com.vietnam.power.domain.PaymentMethodSearchCriteria;
import com.vietnam.power.persitence.AppConfiguration;
import com.vietnam.power.persitence.PaymentMethod;
import com.vietnam.power.persitence.StatisticTable;

@Repository
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public class ApplicationDaoImpl extends GenericDaoImpl implements ApplicationDao {

	public List<AppConfiguration> getAppConfigurations() {
		return findAll(AppConfiguration.class);
	}

	public AppConfiguration getAppConfigurationByName(String name) {
		Criteria crt = getSessionFactory().getCurrentSession().createCriteria(AppConfiguration.class);
		crt.add(Restrictions.eq("name", name));
		crt.setMaxResults(1);

		return (AppConfiguration) crt.uniqueResult();
	}

	public List<PaymentMethod> getPaymentMethod() {
		return findAll(PaymentMethod.class);
	}

	public PaymentMethod getPaymentMethodById(int id) {
		return findById(PaymentMethod.class, id);
	}

	public void createPaymentMethod(PaymentMethod pm) {
		save(pm);
	}

	public void updatePaymentMethod(PaymentMethod pm) {
		merge(pm);
	}

	public void deletePaymentMethod(PaymentMethod pm) {
		delete(pm);
	}

	@SuppressWarnings("unchecked")
	public List<PaymentMethod> searchPaymentMethod(PaymentMethodSearchCriteria s) {
		Criteria crt = getSessionFactory().getCurrentSession().createCriteria(PaymentMethod.class);
		crt.add(Restrictions.ilike("name", s.getName(), MatchMode.ANYWHERE));

		return crt.list();
	}

	public void createOrUpdateStatistic(StatisticTable statisticTable) {
		super.saveOrUpdate(statisticTable);
	}
}
